//
//  TableViewController.m
//  Boom
//
//  Created by 云鹏 赵 on 15/3/6.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "TableViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController
- (void)dealloc
{
    [_dataArray release];
    [_urlStr release];
    [_myTableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    // 获取数据
    self.dataArray = [NSMutableArray array];
    [self afnGetData];

    // 创建tableView
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 64)  style:UITableViewStylePlain];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    [self addHeader];
}
// 下拉刷新
- (void)addHeader
{
    __block TableViewController *tableVC = self;
    [self.myTableView addHeaderWithCallback:^{
        
        [tableVC afnGetData];
        
        //（为了模拟延迟加载数据，因此2秒后才让刷新请求结束，实际项目中可以在数据请求成功后，停止下拉效果）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 结束刷新
            [tableVC.myTableView headerEndRefreshing];
        });
    }];
    // 自动刷新
    [self.myTableView headerBeginRefreshing];
}


// tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"reuse";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [cell autorelease];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SingleModel *singModel = [self.dataArray objectAtIndex:indexPath.row];
    cell.singleModel = singModel;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 125;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[PlayerViewController sharePlayerDataHandel] streamerStop];
    
    SingleModel *singModel = [[SingleModel alloc] init];
    singModel = [self.dataArray objectAtIndex:indexPath.row];
    NSString *string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/track/detail?device=iPhone&trackId=%d", singModel.trackId];
    // afn获取数据
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = string;
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];

        NSMutableDictionary *dictionary = responseObject;
        PlayData *playData = [[PlayData alloc] init];
        [playData setValuesForKeysWithDictionary:dictionary];
        [PlayerViewController sharePlayerDataHandel].playData = playData;
        [[PlayerViewController sharePlayerDataHandel] buttonPressed];
        [playData release];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];

    
    
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:[PlayerViewController sharePlayerDataHandel]];
    [PlayerViewController sharePlayerDataHandel].navigationController.navigationBarHidden = YES;
    [self presentViewController:naVC animated:YES completion:^{
        
        
    }];
    [naVC release];
}
// afn获取数据
- (void)afnGetData
{
    
    self.urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%d/true/1/30?device=iPhone", self.id];
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = self.urlStr;
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        NSMutableDictionary *dictionary = responseObject;
        NSMutableDictionary *dictionary2 = [dictionary objectForKey:@"tracks"];
        NSMutableArray *array = [dictionary2 objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            SingleModel *singleModel = [[SingleModel alloc] init];
            [singleModel setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:singleModel];
            [singleModel release];
        }
        
        [self.myTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
